package com.blog.admin.service;

import com.blog.admin.mapper.BlogArticleMapper;
import com.blog.admin.mapper.BlogClassifyMapper;
import com.blog.admin.mapper.BlogCommentMapper;
import com.blog.admin.vo.BlogArticleVO;
import com.blog.admin.vo.BlogClassifyVO;
import com.blog.admin.vo.BlogCommentVO;
import com.blog.vo.PageVO;
import com.blog.common.PageHelperLog;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @author tangqj
 * @Description 友情链接
 * @create 2020/2/18 14:35
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BlogArticleService {
    @Autowired
    BlogArticleMapper blogArticleMapper;
    @Autowired
    BlogClassifyMapper blogClassifyMapper;
    @Autowired
    BlogCommentMapper blogCommentMapper;
    /**创建线程池*/
    public static ScheduledExecutorService service = Executors.newScheduledThreadPool(50);


    /**
     * 分页获取文章
     *
     * @param page
     * @return
     */
    @PageHelperLog(desc = "msg")
    public PageInfo<BlogArticleVO> getBlogArticle(PageVO page, BlogArticleVO blogArticleVO) {
        List<BlogArticleVO> list = blogArticleMapper.getBlogArticle(blogArticleVO);
        PageInfo<BlogArticleVO> pageRest = new PageInfo<BlogArticleVO>(list);
        return pageRest;
    }

    /**
     * 获取分类列表
     *
     * @return
     */
    public List<BlogClassifyVO> getAllBlogClassify() {
        List<BlogClassifyVO> list = blogClassifyMapper.getBlogClassify(null);
        return list;
    }


    /**
     * 插入文章
     *
     * @param blogArticleVO
     * @return
     */
    public boolean insertBlogArticle(BlogArticleVO blogArticleVO) {
        int result = blogArticleMapper.insertBlogArticle(blogArticleVO);
        return result > 0 ? true : false;
    }

    /**
     * 修改文章
     *
     * @param blogArticleVO
     * @return
     */
    public boolean updateBlogArticle(BlogArticleVO blogArticleVO) {
        int result = blogArticleMapper.updateBlogArticle(blogArticleVO);
        return result > 0 ? true : false;
    }

    /**
     * 修改文章状态
     *
     * @param blogArticleVO
     * @return
     */
    public boolean updateBlogArticleStatus(BlogArticleVO blogArticleVO) {
        int result = blogArticleMapper.updateBlogArticleStatus(blogArticleVO);
        return result > 0 ? true : false;
    }

    /**
     * 置顶
     *
     * @param blogArticleVO
     * @return
     */
    public boolean updateBlogArticleTop(BlogArticleVO blogArticleVO) {
        int result = blogArticleMapper.updateBlogArticleTop(blogArticleVO);
        return result > 0 ? true : false;
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    public boolean deleteBlogArticleById(String id) {
        try {
            int result = blogArticleMapper.deleteBlogArticleById(id);
            if (result < 1) {
                return false;
            }
            result = blogCommentMapper.deleteBlogCommentById(id);
            return result > 0 ? true : false;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键 手动回滚
            return false;
        }

    }

    /**
     * 分页获取文章评论
     *
     * @param page
     * @return
     */
    @PageHelperLog(desc = "msg")
    public PageInfo<BlogCommentVO> getBlogCommentAll(PageVO page, BlogCommentVO blogArticleVO) {
        List<BlogCommentVO> list = blogCommentMapper.getBlogCommentAll(blogArticleVO);
        PageInfo<BlogCommentVO> pageRest = new PageInfo<BlogCommentVO>(list);
        return pageRest;
    }

    /**
     * 删除文章评论
     *
     * @param id
     * @return
     */
    public boolean deleteBlogCommentById(String id) {
        int result = blogCommentMapper.deleteBlogCommentById(id);
        return result > 0 ? true : false;
    }


}
